package com.firstgroup.controller;

import com.firstgroup.Utils.ResponseResult;
import com.firstgroup.domain.Interviewers;
import com.firstgroup.domain.Page;
import com.firstgroup.domain.QuessionBank;
import com.firstgroup.domain.User;
import com.firstgroup.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
@Api(tags = "管理员接口")
public class AdminController {

    @Autowired
    private AdminService adminService;
    private Logger logger = LoggerFactory.getLogger(AdminController.class);

    @ApiOperation(value = "添加用户",notes = "单个添加")
    @ApiImplicitParams(
            {
                @ApiImplicitParam(name = "id",value = "用户id"),
                @ApiImplicitParam(name = "classId",value = "班级"),
                @ApiImplicitParam(name = "name",value = "姓名"),
                @ApiImplicitParam(name = "sex",value = "性别"),
                @ApiImplicitParam(name = "studentId",value = "学号"),
                @ApiImplicitParam(name = "username",value = "用户名（账号）"),
                @ApiImplicitParam(name = "password",value = "密码")
            }
    )
    @PostMapping("/addUser")
    @ResponseBody
    public String  addUser(User user){
        logger.info("添加的用户{}",user);
       return adminService.addUser(user);
    }

    @ApiOperation(value = "批量添加用户",notes = "批量添加用户,异常返回值为null")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id",value = "用户id"),
                    @ApiImplicitParam(name = "classId",value = "班级"),
                    @ApiImplicitParam(name = "name",value = "姓名"),
                    @ApiImplicitParam(name = "sex",value = "性别"),
                    @ApiImplicitParam(name = "studentId",value = "学号"),
                    @ApiImplicitParam(name = "username",value = "用户名（账号）"),
                    @ApiImplicitParam(name = "password",value = "密码")
            }
    )
    @PostMapping("/batchAddUser")
    @ResponseBody
    public List<User> batchAddUser(List<User> list){
        try {
            List<User> lis = adminService.batchAddUser(list);
            for (User user : lis) {
                logger.info("批量添加的用户{}",user);
            }
            return lis;
        } catch (Exception e) {
            logger.info("error",e);
        }
        return null;
    }

    @ApiOperation(value = "删除用户",notes = "单个删除，异常返回值为-1")
    @DeleteMapping("/deleteUser")
    @ResponseBody
    public int deleteUser(Integer id){
        try {
            return adminService.deleteUser(id);
        } catch (Exception e) {
            logger.info("error",e);
        }
        return -1;
    }

    @ApiOperation(value = "批量删除用户",notes = "批量删除用户,返回值是影响行数，参数为Integer类型的数组,异常返回值为-1")
    @DeleteMapping("/batchDeleteUser")
    @ResponseBody
    public int batchDeleteUser(Integer[] id){
        try {
            return adminService.batchDeleteUser(id);
        } catch (Exception e) {
            logger.info("error",e);
        }
        return -1;
    }

    @ApiOperation(value = "全部删除",notes = "全部删除，返回值是影响行数，不需要传值,异常返回值为-1")
    @DeleteMapping("/deleteAllUser")
    @ResponseBody
    public int deleteAllUser(){
        try {
            return adminService.deleteAllUser();
        } catch (Exception e) {
            logger.info("error",e);
        }
        return -1;
    }

    @ApiOperation(value = "初始化密码",notes = "单个初始化密码,返回值是影响行数")
    @PutMapping("/setPassword")
    @ResponseBody
    public int setPassword(Integer id){
        logger.info("初始化面的id{}",id);
        return adminService.setPassword(id);
    }

    @ApiOperation(value = "批量初始化密码",notes = "批量初始化密码,返回值是影响行数，参数为Integer类型的数组,异常返回值为-1")
    @PutMapping("/batchSetPassword")
    @ResponseBody
    public int batchSetPassword(Integer[] id){
        try {
            for (Integer integer : id) {
                logger.info("将要批量初始化密码的id{}",integer);
            }
            return adminService.batchSetPassword(id);
        } catch (Exception e) {
                logger.info("error",e);
        }
        return -1;
    }

    @ApiOperation(value = "全部初始化密码",notes = "全部初始化密码,返回值是影响行数，不需要传值,异常返回值为-1")
    @PutMapping("/setAllPassword")
    @ResponseBody
    public int setAllPassword(){
        try {
            return adminService.setAllPassword();
        } catch (Exception e) {
            logger.info("error",e);
        }
        return -1;
    }

    @ApiOperation(value = "查询所有用户",notes = "显示所有用户，用于渲染页面")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow",value = "当前页码"),
                    @ApiImplicitParam(name = "pageSize",value = "每页显示多少条"),
            }
    )
    @GetMapping("/queryAllUser")
    @ResponseBody
    public Map<String,Object> queryAllUser(int pageNow,int pageSize){
        Page<User> page = adminService.queryAllUser(pageNow,pageSize);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("user",page);
        logger.info("所有用户{}",map);
       return map;
    }

    @ApiOperation(value = "更新用户信息",notes = "User对象，更改的信息不包括用户id")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id",value = "用户id"),
                    @ApiImplicitParam(name = "classId",value = "班级"),
                    @ApiImplicitParam(name = "name",value = "姓名"),
                    @ApiImplicitParam(name = "sex",value = "性别"),
                    @ApiImplicitParam(name = "studentId",value = "学号"),
                    @ApiImplicitParam(name = "username",value = "用户名（账号）"),
                    @ApiImplicitParam(name = "password",value = "密码")
            }
    )
    @GetMapping("/updateUser")
    @ResponseBody
    public User updateUser(User user){
        logger.info("更新用户{}",adminService.updateUser(user).toString());
        return adminService.updateUser(user);
    }

    @ApiOperation(value = "模糊查询用户",notes = "根据用户真实姓名,长度2-4")
    @GetMapping("/findByNameLike")
    @ResponseBody
    public List<User> findByNameLike(String name){
        return adminService.findByNameLike("%"+name+"%");
    }

    @ApiOperation(value = "模糊查询用户",notes = "根据用户昵称")
    @GetMapping("/findByUsernameLike")
    @ResponseBody
    public List<User> findByUsernameLike(String username) {
        return adminService.findByUsernameLike("%"+username+"%");
    }


    //遍历面试题库
    @ApiOperation(value = "查询面试题库",notes = "显示面试题库，用于渲染页面")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow",value = "当前页码"),
                    @ApiImplicitParam(name = "pageSize",value = "每页显示多少条"),
            }
    )
    @GetMapping("/findByInterview")
    @ResponseBody
    public Map<String,Object> findByInterview(int pageNow,int pageSize){
        Page<QuessionBank> interview = adminService.findByType("面试", pageNow, pageSize);
        Map<String,Object> interviewMap = new HashMap<String,Object>();
        interviewMap.put("interviewQuestion",interview);
        logger.info("面试题库{}",interviewMap);
        return interviewMap;
    }

    //遍历笔试题库
    @ApiOperation(value = "查询笔试题库",notes = "显示笔试题库，用于渲染页面")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow",value = "当前页码"),
                    @ApiImplicitParam(name = "pageSize",value = "每页显示多少条"),
            }
    )
    @GetMapping("/findByWrite")
    @ResponseBody
    public Map<String,Object> findByWrite(int pageNow,int pageSize){
        Page<QuessionBank> written  = adminService.findByType("笔试", pageNow, pageSize);
        Map<String,Object> writtenMap = new HashMap<String,Object>();
        writtenMap.put("writtenQuestion",written);
        logger.info("笔试题库{}",writtenMap);
        return writtenMap;
    }

    //添加题目
    @ApiOperation(value = "增加题目（面试题或笔试题）",notes = "题目增加")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id",value = "题目id(不填)"),
                    @ApiImplicitParam(name = "questionName",value = "题目内容"),
                    @ApiImplicitParam(name = "type",value = "题目种类"),
                    @ApiImplicitParam(name = "answer",value = "题目答案"),
            }
    )
    @PostMapping("/addQuestion")
    @ResponseBody
    public QuessionBank addQuestion(QuessionBank questionBank) {
        logger.info("新增题目{}",questionBank);
        return adminService.addQuestion(questionBank);
    }

    @ApiOperation(value = "删除题目",notes = "根据题目id")
    @DeleteMapping("/deleteQuestion")
    @ResponseBody
    public Integer deleteQuestion(int id) {
//        logger.info("删除题目影响行数{}",adminService.deleteQuestion(id));
        return adminService.deleteQuestion(id);
    }

    @ApiOperation(value = "批量删除题目",notes = "根据题目id，传入Integer类型的数组")
    @DeleteMapping("/batchDeleteQuestion")
    @ResponseBody
    public Integer batchDeleteQuestion(Integer[] id) {
        int i = 0;
        for (Integer integer : id) {
            Integer in = adminService.deleteQuestion(integer);
            i++;
        }
        logger.info("删除题目影响行数{}",i);
        return i;
    }

    @ApiOperation(value = "更新题目",notes = "根据题目id")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id",value = "题目id"),
                    @ApiImplicitParam(name = "questionName",value = "题目内容"),
                    @ApiImplicitParam(name = "type",value = "题目种类"),
                    @ApiImplicitParam(name = "answer",value = "题目答案"),
            }
    )
    @PutMapping("/updateQuestion")
    @ResponseBody
    public Integer updateQuestion(int id, String questionName, String type, String answer) {
        logger.info("更新题目内容{}","["+questionName+","+type+","+answer+"]");
        return adminService.updateQuestion(id, questionName, type,answer);
    }

    @ApiOperation(value = "模糊查询题目面试",notes = "根据问题的内容")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "name",value = "题目名称"),
                    @ApiImplicitParam(name = "pageNow",value = "当前页码"),
                    @ApiImplicitParam(name = "pageSize",value = "一页显示数量"),
            }
    )
    @GetMapping("/findByQuestionName")
    @ResponseBody
    public Map<String,Object> findByQuestionName(String name,Integer pageNow,Integer pageSize){
        Page<QuessionBank> byQuestionName = adminService.findByQuestionName(name, pageNow, pageSize);
        Map<String,Object> writtenMap = new HashMap<String,Object>();
        writtenMap.put("writtenQuestion",byQuestionName);
        logger.info("模糊查询面试题库{}",writtenMap);
        return writtenMap;
    }

    @ApiOperation(value = "模糊查询题目笔试",notes = "根据问题的内容")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "name",value = "题目名称"),
                    @ApiImplicitParam(name = "pageNow",value = "当前页码"),
                    @ApiImplicitParam(name = "pageSize",value = "一页显示数量"),
            }
    )
    @GetMapping("/findByQuestionNameW")
    @ResponseBody
    public Map<String,Object> findByQuestionNameW(String name,Integer pageNow,Integer pageSize) {
        Page<QuessionBank> byQuestionName = adminService.findByQuestionNameW(name, pageNow, pageSize);
        Map<String,Object> writtenMap = new HashMap<String,Object>();
        writtenMap.put("writtenQuestion",byQuestionName);
        logger.info("模糊查询笔试题库{}",writtenMap);
        return writtenMap;

    }

    @ApiOperation(value = "模糊查询面试者",notes = "根据面试者的名字")
    @GetMapping("/findInterByName")
    @ResponseBody
    public List<Interviewers> findInterByName(String name) {
        return adminService.findInterByName(name);
    }

    @ApiOperation("根据ID查找用户")
    @ApiImplicitParam(name = "id",value = "用户ID",required = true)
    @GetMapping("/findUserById")
    @ResponseBody
    public ResponseResult findUserById(int id){
        return adminService.findUserById(id);
    }
}
